package com.alibaba.citrus.cr.mn.order.center.forecastorder.convert;

import com.epoch.app.mnordercenter.model.dto.*;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class QueryForecastResponseToResponseConvertor {

    public static QueryForecastOrderResponse convert(QueryForecastResponse queryForecastResponse) {
        QueryForecastOrderResponse response = new QueryForecastOrderResponse();
        if (Objects.isNull(queryForecastResponse)) {
            return response;
        }
        response.setSalesOrderNo(queryForecastResponse.getSalesOrderNo());
        response.setOrderCategoryCode(queryForecastResponse.getOrderCategoryCode());
        //response.setOrderCategoryName(queryForecastResponse.getOrderCategoryName());
        response.setOrderChannelCode(queryForecastResponse.getOrderChannelCode());
        //response.setOrderChannelName(queryForecastResponse.getOrderChannelName());
        response.setFromSys(queryForecastResponse.getFromSys());
        response.setBusinessType(queryForecastResponse.getBusinessType());
        response.setBusinessTypeName(queryForecastResponse.getBusinessTypeName());
        response.setBusinessUnitCode(queryForecastResponse.getBusinessUnitCode());
        response.setBusinessUnitName(queryForecastResponse.getBusinessUnitName());
        response.setSalesOrderType(queryForecastResponse.getSalesOrderType());
        response.setSalesOrderTypeName(queryForecastResponse.getSalesOrderTypeName());
        response.setShipToPartyCode(queryForecastResponse.getShipToPartyCode());
        response.setShipToPartyName(queryForecastResponse.getShipToPartyName());
        response.setCustomerCode(queryForecastResponse.getCustomerCode());
        response.setCustomerName(queryForecastResponse.getCustomerName());
        response.setBusinessChannel(queryForecastResponse.getBusinessChannel());
        response.setBusinessChannelName(queryForecastResponse.getBusinessChannelName());
        response.setSalesOrganizationCode(queryForecastResponse.getSalesOrganizationCode());
        response.setSalesOrganizationName(queryForecastResponse.getSalesOrganizationName());
        response.setSalesChannelCode(queryForecastResponse.getSalesChannelCode());
        response.setSalesChannelName(queryForecastResponse.getSalesChannelName());
        response.setProductGroupCode(queryForecastResponse.getProductGroupCode());
        response.setProductGroupName(queryForecastResponse.getProductGroupName());
        response.setOrderStatus(Objects.nonNull(queryForecastResponse.getOrderStatus()) ? queryForecastResponse.getOrderStatus().toString() : "");
        response.setVaildBeginDate(queryForecastResponse.getVaildBeginDate());
        response.setExpectedDeliveryDate(queryForecastResponse.getExpectedDeliveryDate());
        response.setDeliveryMethodCode(queryForecastResponse.getDeliveryMethodCode());
        response.setDeliveryMethodName(queryForecastResponse.getDeliveryMethodName());
        response.setCreatorCode(queryForecastResponse.getCreatorCode());
        response.setCreatorName(queryForecastResponse.getCreatorName());
        response.setCreateTime(queryForecastResponse.getCreateTime());
        response.setModifierCode(queryForecastResponse.getModifierCode());
        response.setModifierName(queryForecastResponse.getModifierName());
        response.setModifierTime(queryForecastResponse.getModifierTime());
        response.setCustomerPurchaseOrderNo(queryForecastResponse.getCustomerPurchaseOrderNo());
        response.setCustomerPurchaseDate(queryForecastResponse.getCustomerPurchaseDate());
        response.setOrderReasonCode(queryForecastResponse.getOrderReasonCode());
        response.setOrderReasonName(queryForecastResponse.getOrderReasonName());
        response.setNote(queryForecastResponse.getNote());
        //response.setReceiveWarehouseCode(queryForecastResponse.getReceiveWarehouseCode());
        //response.setReceiveWarehouseName(queryForecastResponse.getReceiveWarehouseName());
        //response.setReceiveFactoryCode(queryForecastResponse.getReceiveFactoryCode());
        response.setReceiveFactoryName("");
        //response.setReceiveStockCode(queryForecastResponse.getReceiveStockCode());
        response.setReceiveStockName("");
        response.setShippingWarehouseCode(queryForecastResponse.getShippingWarehouseCode());
        response.setShippingWarehouseName(queryForecastResponse.getShippingWarehouseName());
        response.setFulfillmentTypeCode(queryForecastResponse.getFulfillmentTypeCode());
        response.setFulfillmentPriority(queryForecastResponse.getFulfillmentPriority());
        response.setOrderBizTag(Objects.nonNull(queryForecastResponse.getOrderTag()) ? Arrays.asList(queryForecastResponse.getOrderTag().split(",")) : Lists.newArrayList());
        response.setOutOrderNo(queryForecastResponse.getOuterOrderCode());
        response.setSourceOrderNo(queryForecastResponse.getSourceOrderNo());
        response.setCustomerGrade(queryForecastResponse.getCustomerGrade());
        response.setCustomerGroup(queryForecastResponse.getCustomerGroup());
        response.setCustomerGroupName(queryForecastResponse.getCustomerGroupName());
        response.setBizFeatures(queryForecastResponse.getBizFeatures());
        response.setAutoFulfillment(queryForecastResponse.getAutoFulfillment());
        response.setSalesDepartmentCode(queryForecastResponse.getSalesDepartmentCode());
        response.setSalesDepartmentName(queryForecastResponse.getSalesDepartmentName());
        List<QueryForecastOrderDetailResponse> detailResponseList = new ArrayList<>();
        for (QueryForecastDetailResponseDTO forecastDetailDTO : queryForecastResponse.getForecastDetailDTOS()) {
            QueryForecastOrderDetailResponse detailResponse = new QueryForecastOrderDetailResponse();
            detailResponse.setLineNum(forecastDetailDTO.getLineNum());
            detailResponse.setSourceLineNum(forecastDetailDTO.getSourceLineNum());
            detailResponse.setOutLineNum(forecastDetailDTO.getOuterLineId());
            detailResponse.setStatus(Objects.nonNull(forecastDetailDTO.getStatus()) ? forecastDetailDTO.getStatus().toString() : "");
            //【产品编码】、【产品条码】、【产品名称】待确认
            detailResponse.setInnerProductCode(forecastDetailDTO.getInnerProductCode());
            detailResponse.setInnerProductName(forecastDetailDTO.getProductName());
            detailResponse.setBarCode(forecastDetailDTO.getBarCode());

            detailResponse.setProductCode(forecastDetailDTO.getProductCode());
            detailResponse.setProductName(forecastDetailDTO.getProductName());
            detailResponse.setProductQuantity(forecastDetailDTO.getProductQuantity());
            detailResponse.setSaleUnitCode(forecastDetailDTO.getSalesUnitCode());
            detailResponse.setSaleUnitName(forecastDetailDTO.getSalesUnitName());
            detailResponse.setStockUnitCode(forecastDetailDTO.getStockUnitCode());
            detailResponse.setStockUnitName(forecastDetailDTO.getStockUnitName());
            detailResponse.setUnitConversionRelationship(forecastDetailDTO.getUnitConversionRelationship());
            detailResponse.setShippingWarehouseCode(forecastDetailDTO.getShippingWarehouseCode());
            detailResponse.setShippingWarehouseName(forecastDetailDTO.getShippingWarehouseName());
            detailResponse.setFactoryCode(forecastDetailDTO.getFactoryCode());
            // 无 【交货工厂名称】
            // detailResponse.setFactoryName(forecastDetailDTO.getFactoryName());
            detailResponse.setFactoryName("");
            //detailResponse.setStockCode(forecastDetailDTO.getStockCode());
            // 无 【库存地点名称】
            // detailResponse.setStockName(forecastDetailDTO.getStockName());
            detailResponse.setStockName("");
            detailResponse.setPredictReplyAmount(Objects.nonNull(forecastDetailDTO.getPredictReplyAmount()) ? forecastDetailDTO.getPredictReplyAmount().toString() : "");
            detailResponse.setActualTotalReplyAmount(Objects.nonNull(forecastDetailDTO.getActualTotalReplyAmount()) ? forecastDetailDTO.getActualTotalReplyAmount().toString() : "");
            detailResponse.setProductionDate(forecastDetailDTO.getProductionDate());
            detailResponse.setUnitPrice(forecastDetailDTO.getUnitPrice());
            detailResponse.setTotalAmount(forecastDetailDTO.getTotalAmount());
            detailResponse.setIsClose(Objects.nonNull(forecastDetailDTO.getIsClose()) ? forecastDetailDTO.getIsClose().toString() : "");
            detailResponse.setCloseCode(forecastDetailDTO.getCloseCode());
            detailResponse.setCloseMsg(forecastDetailDTO.getCloseMsg());
            detailResponse.setActivityTotalAmount(forecastDetailDTO.getTotalActivityAmount());
            detailResponse.setActivityPrice(forecastDetailDTO.getActivityPrice());
            detailResponse.setOutLineNum(forecastDetailDTO.getOuterLineId());
            detailResponse.setPredictReplyAmount(Objects.nonNull(forecastDetailDTO.getPredictReplyAmount()) ? forecastDetailDTO.getPredictReplyAmount().toString() : "");
            detailResponse.setNote(forecastDetailDTO.getNote());
            detailResponse.setBizFeatures(forecastDetailDTO.getBizFeatures());

			detailResponseList.add(detailResponse);
		}
		response.setForecastOrderDetailDTOS(detailResponseList);
		return response;
	}

    public static void convert(QueryB2bForecastOrderResponse response, QueryForecastResponse queryForecastResponse) {
		BeanUtils.copyProperties(queryForecastResponse, response);
		List<QueryB2bForecastOrderDetailResponse> queryB2bForecastOrderDetailResponses = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(queryForecastResponse.getForecastDetailDTOS())) {
			queryForecastResponse.getForecastDetailDTOS().forEach(detail -> {
				QueryB2bForecastOrderDetailResponse queryB2bForecastOrderDetail = new QueryB2bForecastOrderDetailResponse();
				BeanUtils.copyProperties(queryForecastResponse, queryB2bForecastOrderDetail);
				queryB2bForecastOrderDetailResponses.add(queryB2bForecastOrderDetail);
			});
			response.setOrderDetailList(queryB2bForecastOrderDetailResponses);
		}
	}
}
